package com.sx.water.modules.Controller;

import com.sx.water.modules.entity.IntegerTimeRainingBo;
import com.sx.water.modules.sever.RainMonitorServer;
import com.sx.water.util.DateUtils;
import com.sx.water.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 监测页面
 * @author: taoxi
 * @create: 2018-12-11 13:22
 * PRE                实时
 * Pre_Sum_30min      近30分钟
 * PRE_1h             近1小时
 * PRE_3h             近3小时
 * PRE_6h             近6小时
 * PRE_12h            近12小时
 * PRE_24h            近24小时
 * PRE_36h            近36小时
 * PRE_48h            近48小时
 * PRE_60h            近60小时
 * PRE_72h            近72小时
 * PRE_08             08时以来降水
 * PRE_20             20时以来降水
 * PRE_01             整点1小时降水
 * PRE_06             整点6小时降水
 * PRE_20             整点20小时降水
 **/
@CrossOrigin
@RestController
@RequestMapping(value ="/monitor")
public class RainMonitorController {

    @Autowired
    RainMonitorServer rainMonitorServer;

    public static IntegerTimeRainingBo getBo() {
        IntegerTimeRainingBo integerTimeRainingBo = new IntegerTimeRainingBo();
        integerTimeRainingBo.setProvince("浙江");
        integerTimeRainingBo.setCity("绍兴");
        integerTimeRainingBo.setCountry("");
        integerTimeRainingBo.setTown("");
        return integerTimeRainingBo;
    }




    /**
     * 降水
     * @param element 要素
     * PRE（实时） Pre_Sum_30min 近30分钟 PRE_1h  近1小时
     *
     * PRE_3h             近3小时
     * PRE_6h             近6小时
     * PRE_12h            近12小时
     * PRE_24h            近24小时
     *
     * PRE_36h            近36小时
     * PRE_48h            近48小时
     * PRE_60h            近60小时
     * PRE_72h            近72小时
     * @param time    时间  20181022100000
     * @return
     */
    @CrossOrigin
    @RequestMapping("/MonitorHour")
    public R MonitorHour(@RequestParam(value = "element") String element, @RequestParam(value = "time") String time) {
        List<Map<String,Object>> mapList=new ArrayList<>();
        IntegerTimeRainingBo integerTimeRainingBo = getBo();
        //截取 time 截取到分钟
        String endTime=time.substring(0,12);
        switch (element){
            case "PRE":
                integerTimeRainingBo.setStartTime(endTime);
                mapList=rainMonitorServer.selectRainMonitorLive(integerTimeRainingBo);
                if(!mapList.isEmpty() && mapList.size()>0){
                    for(int i=0;i<mapList.size();i++){
                        Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                        mapList.get(i).put("Lon",lon/100);
                        Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                        mapList.get(i).put("Lat",lat/100);
                        mapList.get(i).put("tyCh","1");
                    }
                }
                break;
            case "Pre_Sum_30min":
                Map<String,String> map= GetTimeBy24Hour(endTime,30);
                integerTimeRainingBo.setStartTime(map.get("old"));
                integerTimeRainingBo.setEndTime(map.get("now"));
                mapList=rainMonitorServer.selectRainMonitorLiveBy30(integerTimeRainingBo);
                if(!mapList.isEmpty() && mapList.size()>0){
                    for(int i=0;i<mapList.size();i++){
                        Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                        mapList.get(i).put("Lon",lon/100);
                        Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                        mapList.get(i).put("Lat",lat/100);
                        mapList.get(i).put("tyCh","1");
                        mapList.get(i).put("Pre_Sum_30min",mapList.get(i).get("PRE").toString());
                    }
                }
                for(int i=0;i<mapList.size();i++) {
                    Iterator<String> iter = mapList.get(i).keySet().iterator();
                    while (iter.hasNext()) {
                        String key = iter.next();
                        if (key.equals("PRE")) {
                            iter.remove();
                        }
                    }
                }
                break;
            case "PRE_1h":
                Map<String,String> map1h= GetTimeBy24Hour(endTime,1);
                integerTimeRainingBo.setStartTime(map1h.get("old"));
                integerTimeRainingBo.setEndTime(map1h.get("now"));
                mapList=rainMonitorServer.selectRainMonitorLiveBy30(integerTimeRainingBo);
                if(!mapList.isEmpty() && mapList.size()>0){
                    for(int i=0;i<mapList.size();i++){
                        Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                        mapList.get(i).put("Lon",lon/100);
                        Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                        mapList.get(i).put("Lat",lat/100);
                        mapList.get(i).put("tyCh","1");
                        mapList.get(i).put("Pre_Sum_1h",mapList.get(i).get("PRE").toString());
                    }
                }
                for(int i=0;i<mapList.size();i++) {
                    Iterator<String> iter = mapList.get(i).keySet().iterator();
                    while (iter.hasNext()) {
                        String key = iter.next();
                        if (key.equals("PRE")) {
                            iter.remove();
                        }
                    }
                }
                break;
            case "PRE_3h":
                integerTimeRainingBo.setStartTime(endTime);
                integerTimeRainingBo.setWaterStationAddSql("Precipitation3h");
                mapList=rainMonitorServer.selectRainMonitorHour(integerTimeRainingBo);
                if(!mapList.isEmpty() && mapList.size()>0){
                    for(int i=0;i<mapList.size();i++){
                        Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                        mapList.get(i).put("Lon",lon/100);
                        Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                        mapList.get(i).put("Lat",lat/100);
                        mapList.get(i).put("tyCh","1");
                        mapList.get(i).put("PRE_3h",mapList.get(i).get("PRE").toString());
                    }
                }
                for(int i=0;i<mapList.size();i++) {
                    Iterator<String> iter = mapList.get(i).keySet().iterator();
                    while (iter.hasNext()) {
                        String key = iter.next();
                        if (key.equals("PRE")) {
                            iter.remove();
                        }
                    }
                }
                break;
            case "PRE_6h":
                integerTimeRainingBo.setStartTime(endTime);
                integerTimeRainingBo.setWaterStationAddSql("Precipitation6h");
                mapList=rainMonitorServer.selectRainMonitorHour(integerTimeRainingBo);
                if(!mapList.isEmpty() && mapList.size()>0){
                    for(int i=0;i<mapList.size();i++){
                        Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                        mapList.get(i).put("Lon",lon/100);
                        Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                        mapList.get(i).put("Lat",lat/100);
                        mapList.get(i).put("tyCh","1");
                        mapList.get(i).put("PRE_6h",mapList.get(i).get("PRE").toString());
                    }
                }
                for(int i=0;i<mapList.size();i++) {
                    Iterator<String> iter = mapList.get(i).keySet().iterator();
                    while (iter.hasNext()) {
                        String key = iter.next();
                        if (key.equals("PRE")) {
                            iter.remove();
                        }
                    }
                }
                break;
            case "PRE_12h":
                integerTimeRainingBo.setStartTime(endTime);
                integerTimeRainingBo.setWaterStationAddSql("Precipitation12h");
                mapList=rainMonitorServer.selectRainMonitorHour(integerTimeRainingBo);
                if(!mapList.isEmpty() && mapList.size()>0){
                    for(int i=0;i<mapList.size();i++){
                        Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                        mapList.get(i).put("Lon",lon/100);
                        Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                        mapList.get(i).put("Lat",lat/100);
                        mapList.get(i).put("tyCh","1");
                        mapList.get(i).put("PRE_12h",mapList.get(i).get("PRE").toString());
                    }
                }
                for(int i=0;i<mapList.size();i++) {
                    Iterator<String> iter = mapList.get(i).keySet().iterator();
                    while (iter.hasNext()) {
                        String key = iter.next();
                        if (key.equals("PRE")) {
                            iter.remove();
                        }
                    }
                }
                break;
            case "PRE_24h":
                integerTimeRainingBo.setStartTime(endTime);
                integerTimeRainingBo.setWaterStationAddSql("Precipitation24h");
                mapList=rainMonitorServer.selectRainMonitorHour(integerTimeRainingBo);
                if(!mapList.isEmpty() && mapList.size()>0){
                    for(int i=0;i<mapList.size();i++){
                        Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                        mapList.get(i).put("Lon",lon/100);
                        Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                        mapList.get(i).put("Lat",lat/100);
                        mapList.get(i).put("tyCh","1");
                        mapList.get(i).put("PRE_24h",mapList.get(i).get("PRE").toString());
                    }
                }
                for(int i=0;i<mapList.size();i++) {
                    Iterator<String> iter = mapList.get(i).keySet().iterator();
                    while (iter.hasNext()) {
                        String key = iter.next();
                        if (key.equals("PRE")) {
                            iter.remove();
                        }
                    }
                }
                break;
            case "PRE_36h":
                Map<String,String> map36h= GetTimeBy24Hour(endTime,36);
                integerTimeRainingBo.setStartTime(map36h.get("old"));
                integerTimeRainingBo.setEndTime(map36h.get("now"));
                mapList=rainMonitorServer.selectRainMonitorHourMore(integerTimeRainingBo);
                if(!mapList.isEmpty() && mapList.size()>0){
                    for(int i=0;i<mapList.size();i++){
                        Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                        mapList.get(i).put("Lon",lon/100);
                        Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                        mapList.get(i).put("Lat",lat/100);
                        mapList.get(i).put("tyCh","1");
                        mapList.get(i).put("PRE_36h",mapList.get(i).get("PRE").toString());
                    }
                }
                for(int i=0;i<mapList.size();i++) {
                    Iterator<String> iter = mapList.get(i).keySet().iterator();
                    while (iter.hasNext()) {
                        String key = iter.next();
                        if (key.equals("PRE")) {
                            iter.remove();
                        }
                    }
                }
                break;
            case "PRE_48h":
                Map<String,String> maps= GetTimeBy24Hour(endTime,48);
                integerTimeRainingBo.setStartTime(maps.get("old"));
                integerTimeRainingBo.setEndTime(maps.get("now"));
                mapList=rainMonitorServer.selectRainMonitorHourMore(integerTimeRainingBo);
                if(!mapList.isEmpty() && mapList.size()>0){
                    for(int i=0;i<mapList.size();i++){
                        Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                        mapList.get(i).put("Lon",lon/100);
                        Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                        mapList.get(i).put("Lat",lat/100);
                        mapList.get(i).put("tyCh","1");
                        mapList.get(i).put("PRE_48h",mapList.get(i).get("PRE").toString());
                    }
                }
                for(int i=0;i<mapList.size();i++) {
                    Iterator<String> iter = mapList.get(i).keySet().iterator();
                    while (iter.hasNext()) {
                        String key = iter.next();
                        if (key.equals("PRE")) {
                            iter.remove();
                        }
                    }
                }
                break;
            case "PRE_60h":
                Map<String,String> map60= GetTimeBy24Hour(endTime,60);
                integerTimeRainingBo.setStartTime(map60.get("old"));
                integerTimeRainingBo.setEndTime(map60.get("now"));
                mapList=rainMonitorServer.selectRainMonitorHourMore(integerTimeRainingBo);
                if(!mapList.isEmpty() && mapList.size()>0){
                    for(int i=0;i<mapList.size();i++){
                        Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                        mapList.get(i).put("Lon",lon/100);
                        Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                        mapList.get(i).put("Lat",lat/100);
                        mapList.get(i).put("tyCh","1");
                        mapList.get(i).put("PRE_60h",mapList.get(i).get("PRE").toString());
                    }
                }
                for(int i=0;i<mapList.size();i++) {
                    Iterator<String> iter = mapList.get(i).keySet().iterator();
                    while (iter.hasNext()) {
                        String key = iter.next();
                        if (key.equals("PRE")) {
                            iter.remove();
                        }
                    }
                }
                break;
            case "PRE_72h":
                Map<String,String> map72= GetTimeBy24Hour(endTime,72);
                integerTimeRainingBo.setStartTime(map72.get("old"));
                integerTimeRainingBo.setEndTime(map72.get("now"));
                mapList=rainMonitorServer.selectRainMonitorHourMore(integerTimeRainingBo);
                if(!mapList.isEmpty() && mapList.size()>0){
                    for(int i=0;i<mapList.size();i++){
                        Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                        mapList.get(i).put("Lon",lon/100);
                        Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                        mapList.get(i).put("Lat",lat/100);
                        mapList.get(i).put("tyCh","1");
                        mapList.get(i).put("PRE_72h",mapList.get(i).get("PRE").toString());
                    }
                }
                for(int i=0;i<mapList.size();i++) {
                    Iterator<String> iter = mapList.get(i).keySet().iterator();
                    while (iter.hasNext()) {
                        String key = iter.next();
                        if (key.equals("PRE")) {
                            iter.remove();
                        }
                    }
                }
                break;
        }

        return R.ok().put("DS",mapList);
    }

    //站点24小时 趋势（风向/风速/温度/雨量） 分钟数据
    @CrossOrigin
    @RequestMapping("/Monitor24HourTrend")
    public R Monitor24HourTrend(@RequestParam(value = "stationId") String stationId){
         String time=rainMonitorServer.selectGetMaxTime24Hour(stationId);
        Map<String,String> map=GetTimeBy24Hour(time,24);
        IntegerTimeRainingBo integerTimeRainingBo = getBo();
        integerTimeRainingBo.setStationId(stationId);
        integerTimeRainingBo.setStartTime(map.get("old"));
        integerTimeRainingBo.setEndTime(map.get("now"));
        List<Map<String, Object>> mapList=rainMonitorServer.selectRainStation24HourTrend(integerTimeRainingBo);
        for(int i=0;i<mapList.size();i++){
            mapList.get(i).put("ObservTimes",mapList.get(i).get("ObservTimes").toString()+"00");
        }
        return  R.ok().put("DS",mapList);

    }

   //站点24小时 趋势（风向/风速/温度/雨量） 小时数据
    @CrossOrigin
    @RequestMapping("/Monitor24HourTrendInteger")
    public R Monitor24HourTrendInteger(@RequestParam(value = "stationId") String stationId){
        String time=rainMonitorServer.selectGetMaxTime24Hour(stationId);
        Map<String,String> map=GetTimeBy24Hour(time,24);
        IntegerTimeRainingBo integerTimeRainingBo = getBo();
        integerTimeRainingBo.setStationId(stationId);
        integerTimeRainingBo.setStartTime(map.get("old"));
        integerTimeRainingBo.setEndTime(map.get("now"));
        List<Map<String, Object>> mapList=rainMonitorServer.selectRainStation24HourTrendInteger(integerTimeRainingBo);
        for(int i=0;i<mapList.size();i++){
            mapList.get(i).put("ObservTimes",mapList.get(i).get("ObservTimes").toString()+"0000");
        }
        return  R.ok().put("DS",mapList);

    }

    /**
     * 近30分钟 近1小时 时间处理
     * @param time yyyyMMddHHmm
     * @param num
     * @return
     */
    private Map<String,String> GetTimeBy24Hour(String time,int num){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
        Map<String,String> map=new HashMap<>();
        Date date= DateUtils.stringToDate(time,DateUtils.DATE_TIME_PATTERN_5);
        //现在/传入时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if(num == 30 ){
            calendar.add(Calendar.MINUTE,-num);
        }else {
            calendar.add(Calendar.HOUR,-num);
        }
        String oldTime=df.format(calendar.getTime());
        map.put("now",time);
        map.put("old",oldTime);

        return map;
    }


    /**
     * 降水
     * @param element 要素 PRE（实时） Pre_Sum_30min 近30分钟 PRE_1h  近1小时
     * @param time    时间  2018 10 22 10 00 00
     * @return
     */
  /*  @CrossOrigin
    @RequestMapping("/monitorPre")
    public R MonitorMinit(@RequestParam(value = "element") String element, @RequestParam(value = "time") String time) {
        // Map<String, Object> stringListMap = new HashMap<String, Object>();
        List<Map<String,Object>> mapList=new ArrayList<>();
        IntegerTimeRainingBo integerTimeRainingBo = getBo();
        //截取 time 截取到分钟
        String endTime=time.substring(0,12);
        //实况
        if(element.equals("PRE")){
            //Lat,Lon,Station_Id_C,Station_name,PRE
            integerTimeRainingBo.setStartTime(endTime);
            mapList=rainMonitorServer.selectRainMonitorLive(integerTimeRainingBo);
            if(!mapList.isEmpty() && mapList.size()>0){
                for(int i=0;i<mapList.size();i++){
                    Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                    mapList.get(i).put("Lon",lon/100);
                    Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                    mapList.get(i).put("Lat",lat/100);
                    mapList.get(i).put("tyCh","1");
                }
            }
            //近30分钟
        }else if(element.equals("Pre_Sum_30min")){
            //Lat,Lon,Pre_Sum_30min,StationId,Station_name
            Map<String,String> map= GetTimeBy24Hour(endTime,30);
            integerTimeRainingBo.setStartTime(map.get("old"));
            integerTimeRainingBo.setEndTime(map.get("now"));
            mapList=rainMonitorServer.selectRainMonitorLiveBy30(integerTimeRainingBo);
            if(!mapList.isEmpty() && mapList.size()>0){
                for(int i=0;i<mapList.size();i++){
                    Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                    mapList.get(i).put("Lon",lon/100);
                    Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                    mapList.get(i).put("Lat",lat/100);
                    mapList.get(i).put("tyCh","1");
                    mapList.get(i).put("Pre_Sum_30min",mapList.get(i).get("PRE").toString());
                }
            }
        }else if(element.equals("PRE_1h")){
            //Lat,Lon,Pre_Sum_1h,StationId,Station_name
            Map<String,String> map= GetTimeBy24Hour(endTime,1);
            integerTimeRainingBo.setStartTime(map.get("old"));
            integerTimeRainingBo.setEndTime(map.get("now"));
            mapList=rainMonitorServer.selectRainMonitorLiveBy30(integerTimeRainingBo);
            if(!mapList.isEmpty() && mapList.size()>0){
                for(int i=0;i<mapList.size();i++){
                    Float lon=Float.parseFloat(mapList.get(i).get("Lon").toString());
                    mapList.get(i).put("Lon",lon/100);
                    Float lat=Float.parseFloat(mapList.get(i).get("Lat").toString());
                    mapList.get(i).put("Lat",lat/100);
                    mapList.get(i).put("tyCh","1");
                    mapList.get(i).put("Pre_Sum_1h",mapList.get(i).get("PRE").toString());
                }
            }
        }
        return R.ok().put("DS",mapList);
    }*/

}
